package person.bangbang.im.Androidgin.Loopback;

import android.os.Handler;
import android.util.Log;
/**
 * a example loopback IM handler which take care of 
 * receiving message.
 * 
 * @author bangbang.song@gmail.com
 *
 */
public class ImHandler extends Handler {
	public static final String TAG = "ImHandler";
	/**
	 * a msg has been received.
	 */
	public static final int CODE_RCVD_MSG = 1;
	/**
	 * a msg has been sent to buddy. But not be sure whether is arrived.
	 */
	public static final int CODE_SEND_MSG = 2;
	/**
	 * a msg has arrived to buddy.
	 */
	public static final int CODE_ARRV_MSG = 3;
	
	public static final String USER_ID = "user.id";
	
	private LoopbackProtocol mProtocol;
	
	public ImHandler(LoopbackProtocol p) {
		mProtocol = p;
	}
	

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case CODE_RCVD_MSG: {
				String message = (String) msg.obj;
				String userId = msg.getData().getString(USER_ID);
				
				mProtocol.perfomRcvdIm(userId, message);
				
				break;
			}
			case CODE_SEND_MSG: {
				String message = (String) msg.obj;
				String userId = msg.getData().getString(USER_ID);
				
				mProtocol.perfomSendIm(userId, message);
				
				break;
			}
			default:
				
				Log.d(TAG,"Rcvd a unknown message : " + msg);
			
				break;
			};
		}
}
